package AreaService

import (
	toolsDb "gdshop-back-end-go/common/db"
	"gdshop-back-end-go/common/request/BaseReq"
	"gdshop-back-end-go/common/response"
	"gdshop-back-end-go/common/response/AreaResp"
	"github.com/gogf/gf/net/ghttp"
)

func List(r *ghttp.Request, req *BaseReq.I) *response.JsonResponse {
	ctx := r.GetCtx()
	parentCode := r.GetString("parent_code", "")
	codeLen := len(parentCode) + 2
	var areaList []*AreaResp.AreaTree
	err := toolsDb.GetUnSafaTable(ctx, "area_plus").Fields("code,name").
		Where("is_show = 1 AND LENGTH(`code`) = ? AND `code` LIKE ?", codeLen, parentCode+"%").Structs(&areaList)
	if err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	}
	return response.SuccessByRequestMessageData(nil, "成功", areaList)
}
